package code.lewdy.web;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import code.lewdy.web.session.HttpSessionAttributeListenerImpl;

/**
 * PassportUtil Tag:login session cookie
 * 
 * @author liuding,2012-4-19 $LastChangedDate$ $Author$
 */
public class PassportUtil
{

    private static String USER_ID = "uid";

    private static String USER_NAME = "username";

    private static List<Integer> sessionUserIds = new ArrayList<Integer>();

    public static List<Integer> getSessionUserIds()
    {
        sessionUserIds = HttpSessionAttributeListenerImpl.getSessionUserIds();
        return sessionUserIds;
    }

    /**
     * Get id of logined user
     * 
     * @param request
     * @return
     * @author liuding,2012-4-19
     */
    public static int getUserId(HttpServletRequest request)
    {
        if (request.getSession().getAttribute(USER_ID) != null)
            return (Integer) request.getSession().getAttribute(USER_ID);
        return -1;
    }

    /**
     * Get name of logined user
     * 
     * @param request
     * @return
     * @author liuding,2012-4-19
     */
    public static String getUserName(HttpServletRequest request)
    {
        return request.getSession().getAttribute(USER_NAME) != null ? (String) request.getSession()
                .getAttribute(USER_NAME) : null;
    }

    /* *//**
             * remember login information
             * 
             * @author liuding,2012-4-19
             */
    /*
     * public static void login(HttpServletRequest request, int uid, String username) {
     * System.out.println("login=>uid:"+uid); if(request.getSession().getAttribute(USER_ID) !=
     * null){ request.getSession().removeAttribute(USER_ID);
     * request.getSession().setAttribute(USER_ID, uid); }else{
     * request.getSession().setAttribute(USER_ID, uid); }
     * if(request.getSession().getAttribute(USER_NAME) != null){
     * request.getSession().removeAttribute(USER_NAME); request.getSession().setAttribute(USER_NAME,
     * username); }else{ request.getSession().setAttribute(USER_NAME, username); } }
     */

    /**
     * remember login information
     * 
     * @author liuding,2012-4-19
     */
    public static void login(HttpServletRequest request, int uid, String username)
    {
        System.out.println("login=>uid:" + uid);
        request.getSession().setAttribute(USER_ID, uid);
        request.getSession().setAttribute(USER_NAME, username);
    }

    /**
     * clear login session
     * 
     * @param request
     * @author liuding,2012-4-25
     */
    public static void logout(HttpServletRequest request)
    {
        if (request.getSession().getAttribute(WebConstant.SESSION_USER_ID) != null)
        {
            request.getSession().removeAttribute(WebConstant.SESSION_USER_ID);
        }
    }

    /**
     * 强行移除登录会员下线
     * 
     * @param uid
     * @return
     * @author liuding,2012-4-25
     */
    public static boolean removeASessionUid(int uid)
    {
        sessionUserIds = getSessionUserIds();
        if (sessionUserIds.contains(uid))
        {
            sessionUserIds.remove(uid);
            return true;
        }
        return false;
    }

    public static boolean isLogin(HttpServletRequest request)
    {
        int uid = getUserId(request);
        System.out.println("isLogin()=>uid:" + uid);
        for (int i = 0; i < getSessionUserIds().size(); i++)
        {
            System.out.println("sessionId" + i + ":" + getSessionUserIds().get(i));
        }
        if (uid > 0)
        {
            // uid存在并且在列表中没有被移除
            if (getSessionUserIds().contains(uid))
            {
                return true;
            }
        }
        return false;
    }
}
